package drds.global_transaction.resource_manager;

import drds.global_transaction.api.api.BranchTransactionStatus;
import drds.global_transaction.api.exception.GlobalTransactionException;
import drds.global_transaction.api.in_bound_handler.ResourceManagerInboundHandler;
import drds.global_transaction.api.in_bound_handler.TransactionMessageHandler;
import drds.global_transaction.api.message_codec.request_and_response.transaction.branch_transaction.branch_transaction_end_status_report.branch_transaction_commit_status_report.BranchTransactionCommitRequestMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.branch_transaction.branch_transaction_end_status_report.branch_transaction_commit_status_report.BranchTransactionCommitResponseMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.branch_transaction.branch_transaction_end_status_report.branch_transaction_rollback_status_report.BranchTransactionRollbackRequestMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.branch_transaction.branch_transaction_end_status_report.branch_transaction_rollback_status_report.BranchTransactionRollbackResponseMessage;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ResourceManagerHandler extends AbstractResourceManagerHandler implements ResourceManagerInboundHandler, TransactionMessageHandler
{

	private ResourceManager resourceManager = ResourceManager.get();

	@Override
	protected void doBranchTransactionCommit(BranchTransactionCommitRequestMessage branchTransactionCommitRequestMessage, BranchTransactionCommitResponseMessage branchTransactionCommitResponseMessage) throws GlobalTransactionException
	{
		String globalTransactionIdWithAddressAndPort = branchTransactionCommitRequestMessage.getGlobalTransactionIdWithAddressAndPort();
		long branchTransactionId = branchTransactionCommitRequestMessage.getBranchTransactionId();
		String resourceId = branchTransactionCommitRequestMessage.getResourceId();
		String applicationData = branchTransactionCommitRequestMessage.getApplicationData();
		//
		log.info("branch transaction committing: " + globalTransactionIdWithAddressAndPort + " " + branchTransactionId + " " + resourceId + " " + applicationData);
		BranchTransactionStatus branchTransactionStatus = resourceManager.branchTransactionCommit(globalTransactionIdWithAddressAndPort, branchTransactionId, resourceId, applicationData);
		branchTransactionCommitResponseMessage.setBranchTransactionStatus(branchTransactionStatus);
		log.info("branch transaction commit result: " + branchTransactionStatus);

	}

	@Override
	protected void doBranchTransactionRollback(BranchTransactionRollbackRequestMessage branchTransactionRollbackRequestMessage, BranchTransactionRollbackResponseMessage branchTransactionRollbackResponseMessage) throws GlobalTransactionException
	{
		String globalTransactionId = branchTransactionRollbackRequestMessage.getGlobalTransactionIdWithAddressAndPort();
		long branchTransactionId = branchTransactionRollbackRequestMessage.getBranchTransactionId();
		String resourceId = branchTransactionRollbackRequestMessage.getResourceId();
		String applicationData = branchTransactionRollbackRequestMessage.getApplicationData();
		//
		log.info("branch transaction  rolling back: " + globalTransactionId + " " + branchTransactionId + " " + resourceId);
		BranchTransactionStatus branchTransactionStatus = resourceManager.branchTransactionRollback(globalTransactionId, branchTransactionId, resourceId, applicationData);
		branchTransactionRollbackResponseMessage.setBranchTransactionStatus(branchTransactionStatus);
		log.info("branch transaction rollback result: " + branchTransactionStatus);

	}
}
